/*************************************************************
 * Title: AccessContext.java
 * Description: 
 * Author: Huang Shaobin
 * Email: huangshaobin@qtt.cn
 * CreateTime: Jan 31, 2013 2:44:06 AM
 * Copyright © 北京全天通信息咨询服务有限公司 All right reserved
 ************************************************************/
package snow.core.context;

import java.util.HashMap;
import java.util.Map;

/**
 *
 */
public class AccessContext {

	private static final ThreadLocal<AccessContext> THREAD_LOCAL = new ThreadLocal<AccessContext>();

	public static AccessContext get() {
		AccessContext context = THREAD_LOCAL.get();
		if (context == null) {
			context = new AccessContext();
			THREAD_LOCAL.set(context);
		}
		return context;
	}

	public static void remove() {
		THREAD_LOCAL.remove();
	}

	private final Map<String, Object> container;

	private AccessContext() {
		container = new HashMap<String, Object>();
	}

	public void clear() {
		container.clear();
	}

	public Object get(String key) {
		return container.get(key);
	}

	public <V> V get(String key, Class<V> clazz) {
		final Object value = get(key);
		if (clazz != null && clazz.isInstance(value)) {
			return clazz.cast(value);
		}
		return null;
	}

	public void put(String key, Object value) {
		container.put(key, value);
	}

	public Object remove(String key) {
		return container.remove(key);
	}

}
